package com.tsingteng.cosfun.ui.cosfun;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.AttentFansbean;
import com.tsingteng.cosfun.bean.AttentRecommendbean;
import com.tsingteng.cosfun.bean.FollowBean;
import com.tsingteng.cosfun.bean.RecommenResp;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.cosfun.adapter.AtListAdapter;
import com.tsingteng.cosfun.ui.cosfun.contract.AttentContract;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.RecyclerOnScrollListener;
import com.tsingteng.cosfun.ui.cosfun.presenter.AttantPresenter;
import com.tsingteng.cosfun.ui.focus.adapter.FocusAttentionAdapter;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.ToastUtils;
import com.tsingteng.cosfun.widget.dialog.BaseBottomSheetFragment;

import java.util.ArrayList;
import java.util.List;

public class AtFragment extends BaseBottomSheetFragment implements AttentContract.IAttentView, AtListAdapter.OnItemClickListener ,FocusAttentionAdapter.FocusAttentListener{

    // TODO: Customize parameter argument names
    private static final String ARG_ITEM_COUNT = "item_count";
    AttantPresenter attantPresenter = null;
    int pageNo = 1;
    List<AttentFansbean.Data> atList = null;
    AtListAdapter atListAdapter = null;
    String workId = "";
    RecyclerView recyclerView = null;
    TextView empty_tv;
    TextView at_tv;
    LinearLayout mEmptyLl;
    ListView mListView;
    OnItemSelectLisenter onItemSelectLisenter = null;
    private FocusAttentionAdapter mAttentAdapter;

    // TODO: Customize parameters
    public static AtFragment newInstance() {
        final AtFragment fragment = new AtFragment();
        final Bundle args = new Bundle();
        args.putInt(ARG_ITEM_COUNT, 20);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnItemSelectLisenter(OnItemSelectLisenter onItemSelectLisenter) {
        this.onItemSelectLisenter = onItemSelectLisenter;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 在这里将view的高度设置为精确高度，即可屏蔽向上滑动不占全屏的手势。
        View view = inflater.inflate(R.layout.fragment_at, container, false);
//        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//                AppUtils.getScreenWidthAndHeight(getActivity())[1] / 5 * 2));
        attantPresenter = new AttantPresenter(this);
        atList = new ArrayList<>();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerView = view.findViewById(R.id.at_recycler);
        empty_tv = view.findViewById(R.id.at_empty);
        mListView = view.findViewById(R.id.list_view);
        mEmptyLl = view.findViewById(R.id.empty_ll);
        at_tv = view.findViewById(R.id.at_haoyou);
        ImageView close_img = view.findViewById(R.id.at_close);
        atListAdapter = new AtListAdapter(getActivity(), atList, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(atListAdapter);

        at_tv.setText("@好友");
        close_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView.addOnScrollListener(onScrollListener);
        setState(AtListAdapter.FooterState.Normal);
        attantPresenter.getAttentList(LogonUtils.getProFildId() + "", pageNo + "");

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showFail(String msg) {

    }

    @Override
    public void showError() {

    }

    @Override
    public void showEmpty() {

    }


    protected AtListAdapter.FooterState mState = AtListAdapter.FooterState.Normal;

    protected void setState(AtListAdapter.FooterState mState) {
        this.mState = mState;
        (getActivity()).runOnUiThread(() -> changeAdaperState());
    }

    //改变底部bottom的样式
    protected void changeAdaperState() {
        if (atListAdapter != null && atListAdapter.mFooterHolder != null) {
            atListAdapter.mFooterHolder.setStatus(mState);
        }
    }

    RecyclerOnScrollListener onScrollListener = new RecyclerOnScrollListener() {
        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            if (atListAdapter.mFooterHolder.status == AtListAdapter.FooterState.TheEnd) {
                return;
            }

            attantPresenter.getAttentList(LogonUtils.getProFildId() + "", pageNo + "");

            setState(AtListAdapter.FooterState.Loading);
        }
    };

    @Override
    public void success(AttentFansbean attentFansbean) {
        atList.addAll(attentFansbean.data);
        atListAdapter.notifyDataSetChanged();
        if (atList.size() == 0) {
            recyclerView.setVisibility(View.INVISIBLE);
            mEmptyLl.setVisibility(View.VISIBLE);
            attantPresenter.getCommentInfo();
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            mEmptyLl.setVisibility(View.INVISIBLE);
            if (attentFansbean.data == null || attentFansbean.data.size() == 0) {
                setState(AtListAdapter.FooterState.TheEnd);
            }
            if (atList.size() < Config.PAGE_SIZE) {
                setState(AtListAdapter.FooterState.TheEnd);
            }
            pageNo++;
        }
    }

    @Override
    public void failure(int code, String error) {

    }

    @Override
    public void onItemClick(int position) {
        if (onItemSelectLisenter != null) {
            onItemSelectLisenter.selected(atList.get(position));
        }
    }

    @Override
    public void attentFocus(int position, AttentRecommendbean bean) {
        if (null == bean){
            return;
        }
        if (0 == mAttentAdapter.getItem(position).isAttention){
            mAttentAdapter.getItem(position).isAttention = 1;
            mAttentAdapter.notifyDataSetChanged();
            attantPresenter.loginFollowFans(bean.profileId + "");
        }else{
            mAttentAdapter.getItem(position).isAttention = 0;
            mAttentAdapter.notifyDataSetChanged();
            attantPresenter.loginNoFollow(bean.profileId+"",position);
        }
    }

    @Override
    public void clickimage(int position, AttentRecommendbean bean) {
        if (null == bean){
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putInt("user_id", bean.profileId);
        bundle.putInt(Constant.TAG, 1);
        TerminalActivity.showFragment(getContext(), HomeMineFragment.class, bundle);
    }

    public interface OnItemSelectLisenter {
        void selected(AttentFansbean.Data data);
    }

    @Override
    public void showAttentList(RecommenResp recommenResp) {
        if (null ==recommenResp ||null ==recommenResp.getData()||0 ==recommenResp.getData().size()){
            return;
        }
        mAttentAdapter =new FocusAttentionAdapter(getContext(),recommenResp.getData(),this);
        mListView.setAdapter(mAttentAdapter);
    }

    @Override
    public void getFollowResult(FollowBean bean) {
        if (null ==bean){
            return;
        }
        ToastUtils.showToast(bean.getRelationShip());
    }

    @Override
    public void showNoFollowResult(FollowBean bean) {
        Toast.makeText(getContext(), "取消关注成功", Toast.LENGTH_LONG).show();
    }
}
